*Robust sorting results*

*Ambition*
clear

use "Results\fig_2\results_ambition.dta"

forvalues i=1981(1)2018{
gen temp=lambda if aar==`i'
egen temp2=max(temp)

gen temp3=s_1_1*(temp2*male_1+(1-temp2)*female_1)+s_2_2*(temp2*male_2+(1-temp2)*female_2)+s_3_3*(temp2*male_3+(1-temp2)*female_3)+s_4_4*(temp2*male_4+(1-temp2)*female_4)

qui sum temp3 if aar==1980
sca base=r(mean)

qui sum temp3 if aar==`i'
sca end=r(mean)

sca change_am_`i'=((end-base)/base)*100

drop temp temp2 temp3
}

*Use 2018 lambda for all years

gen temp=lambda if aar==2018
egen temp2=max(temp)

gen temp3=s_1_1*(temp2*male_1+(1-temp2)*female_1)+s_2_2*(temp2*male_2+(1-temp2)*female_2)+s_3_3*(temp2*male_3+(1-temp2)*female_3)+s_4_4*(temp2*male_4+(1-temp2)*female_4)

forvalues i=1980(1)2018{
	qui sum temp3 if aar==`i'
	sca diag_2018_l_am_`i'=r(mean)
}


drop temp temp2 temp3

*Use lambda=0 for all years

gen temp2=0

gen temp3=s_1_1*(temp2*male_1+(1-temp2)*female_1)+s_2_2*(temp2*male_2+(1-temp2)*female_2)+s_3_3*(temp2*male_3+(1-temp2)*female_3)+s_4_4*(temp2*male_4+(1-temp2)*female_4)

forvalues i=1980(1)2018{
	qui sum temp3 if aar==`i'
	sca diag_l0_am_`i'=r(mean)
}

drop temp2 temp3



*Educ*
clear

use "Results\fig_2\results_educ.dta"

forvalues i=1981(1)2018{
gen temp=lambda if aar==`i'
egen temp2=max(temp)

gen temp3=s_1_1*(temp2*male_1+(1-temp2)*female_1)+s_2_2*(temp2*male_2+(1-temp2)*female_2)+s_3_3*(temp2*male_3+(1-temp2)*female_3)+s_4_4*(temp2*male_4+(1-temp2)*female_4)

qui sum temp3 if aar==1980
sca base=r(mean)

qui sum temp3 if aar==`i'
sca end=r(mean)

sca change_educ_`i'=((end-base)/base)*100

drop temp temp2 temp3
}

*Use 2018 lambda for all years

gen temp=lambda if aar==2018
egen temp2=max(temp)

gen temp3=s_1_1*(temp2*male_1+(1-temp2)*female_1)+s_2_2*(temp2*male_2+(1-temp2)*female_2)+s_3_3*(temp2*male_3+(1-temp2)*female_3)+s_4_4*(temp2*male_4+(1-temp2)*female_4)

forvalues i=1980(1)2018{
	qui sum temp3 if aar==`i'
	sca diag_2018_l_educ_`i'=r(mean)
}

drop temp temp2 temp3

*Use lambda=0 for all years

gen temp2=0

gen temp3=s_1_1*(temp2*male_1+(1-temp2)*female_1)+s_2_2*(temp2*male_2+(1-temp2)*female_2)+s_3_3*(temp2*male_3+(1-temp2)*female_3)+s_4_4*(temp2*male_4+(1-temp2)*female_4)

forvalues i=1980(1)2018{
	qui sum temp3 if aar==`i'
	sca diag_l0_educ_`i'=r(mean)
}

drop temp2 temp3



*field*
clear

use "Results\fig_2\results_field.dta"

forvalues i=1981(1)2018{
gen temp=lambda if aar==`i'
egen temp2=max(temp)

gen temp3=s_1_1*(temp2*male_1+(1-temp2)*female_1)+s_2_2*(temp2*male_2+(1-temp2)*female_2)+s_3_3*(temp2*male_3+(1-temp2)*female_3)+s_4_4*(temp2*male_4+(1-temp2)*female_4)+s_5_5*(temp2*male_5+(1-temp2)*female_5)+s_6_6*(temp2*male_6+(1-temp2)*female_6)+s_7_7*(temp2*male_7+(1-temp2)*female_7)+s_8_8*(temp2*male_8+(1-temp2)*female_8)

qui sum temp3 if aar==1980
sca base=r(mean)

qui sum temp3 if aar==`i'
sca end=r(mean)

sca change_field_`i'=((end-base)/base)*100

drop temp temp2 temp3
}

*Use 2018 lambda for all years

gen temp=lambda if aar==2018
egen temp2=max(temp)

gen temp3=s_1_1*(temp2*male_1+(1-temp2)*female_1)+s_2_2*(temp2*male_2+(1-temp2)*female_2)+s_3_3*(temp2*male_3+(1-temp2)*female_3)+s_4_4*(temp2*male_4+(1-temp2)*female_4)+s_5_5*(temp2*male_5+(1-temp2)*female_5)+s_6_6*(temp2*male_6+(1-temp2)*female_6)+s_7_7*(temp2*male_7+(1-temp2)*female_7)+s_8_8*(temp2*male_8+(1-temp2)*female_8)

forvalues i=1980(1)2018{
	qui sum temp3 if aar==`i'
	sca diag_2018_l_field_`i'=r(mean)
}

drop temp temp2 temp3

*Use lambda=0 for all years

gen temp2=0

gen temp3=s_1_1*(temp2*male_1+(1-temp2)*female_1)+s_2_2*(temp2*male_2+(1-temp2)*female_2)+s_3_3*(temp2*male_3+(1-temp2)*female_3)+s_4_4*(temp2*male_4+(1-temp2)*female_4)+s_5_5*(temp2*male_5+(1-temp2)*female_5)+s_6_6*(temp2*male_6+(1-temp2)*female_6)+s_7_7*(temp2*male_7+(1-temp2)*female_7)+s_8_8*(temp2*male_8+(1-temp2)*female_8)

forvalues i=1980(1)2018{
	qui sum temp3 if aar==`i'
	sca diag_l0_field_`i'=r(mean)
}

drop temp2 temp3



*Plot graphs*

gen diag_l0_am=.
forvalues i=1980(1)2018{
replace diag_l0_am=diag_l0_am_`i' if aar==`i'
}

gen diag_l0_educ=.
forvalues i=1980(1)2018{
replace diag_l0_educ=diag_l0_educ_`i' if aar==`i'
}

gen diag_l0_field=.
forvalues i=1980(1)2018{
replace diag_l0_field=diag_l0_field_`i' if aar==`i'
}

twoway line diag_l0_educ aar, lpattern(dash) lcolor(maroon)  || ///
line diag_l0_field aar, lpattern(longdash) lcolor(dkorange) || ///
line diag_l0_am aar, lpattern(dash_dot) lcolor(forest_green) ///
xtitle("year") ytitle("degree of assortative mating") ///
graphregion(fcolor(white)) yscale(range(1 2.8)) ylabel(1(0.4)2.8) ///
legend(rows(2) cols(2) order(1 "Educational Level" 2 "Educational Field" 3 "Educational Ambition") position(6))

*Generate index
gen temp=diag_l0_am if aar==1980  
egen temp2=max(temp)
gen am_i=(diag_l0_am/temp2)*100  
drop temp temp2

gen temp=diag_l0_educ if aar==1980  
egen temp2=max(temp)
gen educ_i=(diag_l0_educ/temp2)*100  
drop temp temp2

gen temp=diag_l0_field if aar==1980  
egen temp2=max(temp)
gen field_i=(diag_l0_field/temp2)*100  
drop temp temp2


twoway line educ_i aar, lpattern(dash) lcolor(maroon)  || ///
line field_i aar, lpattern(longdash) lcolor(dkorange) || ///
line am_i aar, lpattern(dash_dot) lcolor(forest_green) ///
xtitle("year") ytitle("assortative mating index (1980=100)") ///
graphregion(fcolor(white)) yscale(range(95 120)) ylabel(90(5)120) ///
legend(rows(2) cols(2) order(1 "Educational Level" 2 "Educational Field" 3 "Educational Ambition") position(6))


